﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.MODEL;
using System.Globalization;
using System.Data;
using KPIS.DBM;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class Test1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            switch (rblType.SelectedValue)
            {
                #region payments
                case "fim_payments":
                    IBLL_FIM_Payments pay = new B_FIM_Payments();
                    var models = pay.GetFull(null, null, null, null, null);

                    foreach (var model in models)
                    {
                        model.ref_no = null;
                        pay.Update(model);
                    }

                    for (int i = 0; i < models.Count; i++)
                    {
                        var model = models[i];
                        var date = model.pay_date;

                        if (date.HasValue)
                        {
                            int buddishtYear = new ThaiBuddhistCalendar().GetYear(date.Value);
                            string strCodeDate = "57"
                                + date.Value.Month.ToString("00");
                            string strResult = "";
                            string strPrefix = "";
                            string strMaxCode = "";
                            string strBgmSourceNo = model.bgm_source_seq == 1 ? "1" : "2";

                            strPrefix = "PQ";
                            strMaxCode = FimPaymentsRefNo().Where(m => (m.ReferenceNumber ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).Max(m => m.ReferenceNumber);

                            if (string.IsNullOrEmpty(strMaxCode))
                            {
                                strResult = strPrefix + strBgmSourceNo + strCodeDate + "0001";
                            }
                            else
                            {
                                var strRunning = strMaxCode.Substring(strMaxCode.Length - 4);
                                int nRunning;
                                if (int.TryParse(strRunning, out nRunning))
                                {
                                    strResult = strPrefix + strBgmSourceNo + strCodeDate + (nRunning + 1).ToString("0000");
                                }
                                else
                                {
                                    strResult = strPrefix + strBgmSourceNo + strCodeDate + "0001";
                                }
                            }

                            model.ref_no = strResult;
                            pay.Update(model);
                        }
                    }
                    break;
                #endregion

                #region receive
                case "fim_receives":
                    IBLL_FIM_Receives rec = new B_FIM_Receives();
                    var modelRecs = rec.GetFull(null, null, null);

                    foreach (var model in modelRecs)
                    {
                        model.ref_no = null;
                        rec.Update(model);
                    }

                    for (int i = 0; i < modelRecs.Count; i++)
                    {
                        var model = modelRecs[i];
                        var date = model.receive_date;

                        if (date.HasValue)
                        {
                            int buddishtYear = new ThaiBuddhistCalendar().GetYear(date.Value);
                            string strCodeDate = "57"
                                + date.Value.Month.ToString("00");
                            string strResult = "";
                            string strPrefix = "";
                            string strMaxCode = "";
                            string strBgmSourceNo = model.bgm_source_seq == 1 ? "1" : "2";

                            strPrefix = "RC";
                            var bb = FimReceiptNo().Select(m => m.ReferenceNumber).ToList();
                            var aa = FimReceiptNo().Where(m => (m.ReferenceNumber ?? "").StartsWith(strPrefix + strBgmSourceNo + strCodeDate)).ToList();
                            strMaxCode = aa.Max(m => m.ReferenceNumber);

                            if (string.IsNullOrEmpty(strMaxCode))
                            {
                                strResult = strPrefix + strBgmSourceNo + strCodeDate + "0001";
                            }
                            else
                            {
                                var strRunning = strMaxCode.Substring(strMaxCode.Length - 4);
                                int nRunning;
                                if (int.TryParse(strRunning, out nRunning))
                                {
                                    strResult = strPrefix + strBgmSourceNo + strCodeDate + (nRunning + 1).ToString("0000");
                                }
                                else
                                {
                                    strResult = strPrefix + strBgmSourceNo + strCodeDate + "0001";
                                }
                            }

                            model.ref_no = strResult;
                            rec.Update(model);
                        }
                    }
                    break;
                #endregion
            }            
        }

        public class AutogenerateNumberInfo
        {
            public int Seq { get; set; }
            public string ReferenceNumber { get; set; }
        }

        protected IList<AutogenerateNumberInfo> FimPaymentsRefNo()
        {
            DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

            string strSQL;
            DataRowCollection drc = null;
            IList<AutogenerateNumberInfo> infoList = new List<AutogenerateNumberInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "select pay_seq, ref_no from fim_payments where record_status NOT Like @delete_status; ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        AutogenerateNumberInfo info = new AutogenerateNumberInfo();
                        info.Seq = Convert.ToInt32(drc[i]["pay_seq"]);
                        info.ReferenceNumber = drc[i]["ref_no"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<AutogenerateNumberInfo> FimReceiptNo()
        {
            DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

            string strSQL;
            DataRowCollection drc = null;
            IList<AutogenerateNumberInfo> infoList = new List<AutogenerateNumberInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "select receive_seq, ref_no from fim_receives/* where record_status NOT Like @delete_status*/";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        AutogenerateNumberInfo info = new AutogenerateNumberInfo();
                        info.Seq = Convert.ToInt32(drc[i]["receive_seq"]);
                        info.ReferenceNumber = drc[i]["ref_no"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        protected void cmdPrintExcel_Click(object sender, EventArgs e)
        {
            MailMerge mailMerge = new MailMerge();

            Dictionary<string, string> dic = new Dictionary<string, string>();

            dic.Add("Dic!A:1", "55555");

            mailMerge.MergeExcel(Response, Server.MapPath("~/Reports/Templates/Test.xlsx"), string.Format("Test-{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")), null, dic, null, "asdasdasdasd");
        }
    }
}